const request = require('../../request/index')
let cheerio = require('cheerio')
const {HotWordResultData} = require('../../resultData')

async function getHotKeyword() {
    let url = `https://top.baidu.com/board?tab=realtime`
    let res = await request.get(url)
    let hotKeywords = {
        time: new Date().getTime(),
        list: []
    }
    if (res === undefined) return hotKeywords
    let $ = cheerio.load(res)
    let targetDOMList = $('div[class^="category-wrap"]')
    let results = []

    for (const targetDOMListElement of targetDOMList) {
        results.push(HotWordResultData.generatorResult({
            title: $(targetDOMListElement).find('.c-single-text-ellipsis').text(),
            link: $(targetDOMListElement).find('a').attr('href'),
            subTitle: $($(targetDOMListElement).find('div[class^="hot-desc"]')[0]).text(),
            firstPic: $(targetDOMListElement).find('a>img').attr('src')
        }))
    }
    results = results.reverse()
    hotKeywords.list = results
    return hotKeywords
}

module.exports = getHotKeyword